package org.example.student_dormitory.service.impl;

import org.example.student_dormitory.dao.BuildingManagerDao;
import org.example.student_dormitory.dao.impl.BuildingManagerDaoImpl;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.entity.Admin;
import org.example.student_dormitory.entity.Building;
import org.example.student_dormitory.entity.Dormitory;
import org.example.student_dormitory.service.BuildingManagerService;

import java.util.List;

public class BuildingManagerServiceImpl implements BuildingManagerService {
    private BuildingManagerDao buildingManagerDao = new BuildingManagerDaoImpl();

    //加载首页数据
    @Override
    public Result loadBuildings(String page, String pageSize, String key, String value) {
        int page_int = Integer.parseInt(page);
        int pageSize_int = Integer.parseInt(pageSize);
        //根据value值来判断是否是有条件的分页
        List<Building> list = null;
        if ("".equals(value) || value == null) {
            list = buildingManagerDao.selectAll(page_int,pageSize_int);
        }else {
            list = buildingManagerDao.selectAll(page_int,pageSize_int,key,value);
        }
        if (list == null || list.size() == 0) {
            return new Result<>(100,"没有数据",null);
        }else {
            return new Result<>(200,"有数据",list);
        }
    }

    @Override
    public Result loadAmins() {
        //加载管理员列表
        List<Admin> adminList = buildingManagerDao.selectAllAdmin();
        if (adminList == null || adminList.size() == 0){
            return new Result<>(100,"没有管理员数据",null);
        }else {
            return new Result<>(200,"管理员数据",adminList);
        }
    }

    @Override
    public Result addBuilding(String name, String explain, String adminId) {
        //添加楼宇信息
        Building building = new Building();
        building.setName(name);
        building.setExplain(explain);
        building.setAdminId(Long.parseLong(adminId));
        building.setDr(0);
        boolean insert = buildingManagerDao.insert(building);
        if (insert){
            return new Result<>(200,"有数据",null);
        }else {
            return new Result<>(100,"没有数据",null);
        }
    }

    @Override
    public Result updateBuilding(String id, String buildingInfo) {
        Building building = new Building();
        building.setId(Long.parseLong(id));
        building.setExplain(buildingInfo);
//        building.setAdminId(Long.parseLong(adminId));

        System.out.println("准备进入Dao");
        boolean update = buildingManagerDao.update(building);
        if (update){
            System.out.println("修改成功");
            return new Result<>(200,"修改成功",null);
        }else {
            System.out.println("修改失败");
            return new Result<>(100,"修改失败",null);
        }
    }

    @Override
    public Result deleteById(String id) {
        Boolean delete = buildingManagerDao.deleteById(Integer.parseInt(id));
        if (delete){
            return new Result<>(200,"删除成功", null);
        }else {
            return new Result<>(100,"删除失败", null);
        }
    }
}
